package com.vcredit.creditcat.service.speedloan.xyh;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.type.TypeReference;
import com.vcredit.creditcat.dto.speedloan.CheckCustResultDto;
import com.vcredit.creditcat.model.Cust;
import com.vcredit.creditcat.model.ProductCheckcustItem;
import com.vcredit.creditcat.service.speedloan.CommonProductCheckCustService;
import com.vcredit.creditcat.service.speedloan.ProductCheckCustServiceAdaptor;
import com.vcredit.creditcat.vo.product.xyh.QueryParamVo;
import com.vcredit.creditcat.vo.product.xyh.QueryResultVo;
import com.vcredit.creditcat.vo.product.xyh.ResultVo;
import com.vcredit.framework.annotation.Log;
import com.vcredit.framework.util.HttpRequest;
import com.vcredit.framework.util.JacksonUtil;

@Service(ProductCheckCustServiceAdaptor.BEANNAME_PREFIX + 6)
public class XYHCheckCustService extends CommonProductCheckCustService {
	private static final Integer TIMEOUT = 60; // 请求超时时间60s
	@Value("${product.xyh.queryUrl}")
	private String queryUrl;
	
	@Log
	@Override
	public CheckCustResultDto handlerCheckItem(ProductCheckcustItem checkItem,
			int itemType, Cust cust, Long productId) {
		int checkItemId = checkItem.getCheckItemId();
		if(itemType == 0) { // 原始认证项
			switch (checkItemId) {
			case 1:
				// 执行高级实名完成后的校验规则
				QueryParamVo queryParam = new QueryParamVo(cust.getCustIdno(), cust.getCustName());
				try {
					String queryResultStr = HttpRequest.doJson(queryUrl, JacksonUtil.Obj2Json(queryParam), TIMEOUT);					 			
					ResultVo<QueryResultVo> result = JacksonUtil.Json2Obj(queryResultStr, new TypeReference<ResultVo<QueryResultVo>>(){});					
					if(!result.getStatus().equals("000")) {//接口返回失败默认通过，防止信用花代码异常影响后续流程
						return CheckCustResultDto.success();
					}
					if(result.getData()!=null){
						QueryResultVo content = result.getData();
						// 处理黑名单、灰名单
						if("1".equals(content.getListType())) {
							return CheckCustResultDto.fail("不符合信用花贷款条件");
						} else if("2".equals(content.getListType())) {
							return CheckCustResultDto.fail("暂不符合信用花贷款条件");
						}
						//撞库
						if(!"502".equalsIgnoreCase(content.getChannel()) && !"503".equalsIgnoreCase(content.getChannel()) && cust.getCustIdno().equals(content.getCustIdno())) {									
							return CheckCustResultDto.fail("已在信用花注册");
						}
					}
				} catch (Exception e) {  //接口异常默认通过，防止网络等原因影响后续流程
					return CheckCustResultDto.success();
				} 
				break;
			case 2:
				// 执行手机认证完成后的校验规则
				break;

			default:
				break;
			}
		} else { // 补充认证项
			switch (checkItemId) {
			case 1:
				// 执行信用花基本信息完成后的校验规则
				break;
			case 2:
				// 执行信用花的手机认证完成后的校验规则
				break;

			default:
				break;
			}
		}
		
		return CheckCustResultDto.success();
	}
}
